Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
Unhandled Runtime Error => TypeError: Cannot read properties of undefined (reading 'setState')

I ran into this Runtime error which says setState is undefined as I tried to connect to my Rinkeby test network on Metamask. I have tried binding using constructor props but that is not working. I am at loss what to do. Here's my code:

import React, { Component } from 'react'
import { Button, Form, Input, Grid, Message } from 'semantic-ui-react'
import Layout from '../../components/Layout'
import factory from '../../ethereum/factory'
import web3 from '../../ethereum/web3'
import { Router } from '../../routes'

class New extends React.Component {


constructor(props) {
super(props);
this.state = {
    minimumContribution: '',
    errorMessage: '',
    loading: false
  }
this.onSubmit = this.onSubmit.bind(this);
  }

onSubmit = async event => {
event.preventDefault()
const accounts = await web3.eth.getAccounts()
this.setState({ loading: true, errorMessage: '' })

try {
  await factory.methods.createCampaign(this.state.minimumContribution).send({
    from: accounts[0]
  });

  Router.pushRoute('/')
} catch (error) {
  this.setState({ errorMessage: error.message })
}
this.setState({ loading: false })
}

render() {
const { errorMessage, loading } = this.state
return (
  <Layout>
    <Grid centered columns={2}>
      <Grid.Column>
        <h3>Create a Campaign.</h3>
        <Form onSubmit={this.onSubmit} error={!!errorMessage}>
          <Form.Field>
            <lable>Minimum Contribution </lable>
            <Input
              label="WEI"
              labelPosition="right"
              value={this.state.minimumContribution}
              onChange={event=>this.setState({minimumContribution:event.target.value})}
            />
          </Form.Field>
          <Message error header="Oops!" content={errorMessage}/>
          <Button loading={loading} primary>Create</Button>
        </Form>
      </Grid.Column>
    </Grid>
  </Layout>
    )
   }
  }

  export default New

If it does help I am following an ethereum course on Udemy (Ethereum and Solidity: The Complete Developer's Guide). The error occurs at lecture 190.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Looks like the 'this' context is being lost.

add a new method using arrow function

handleChange = event => this.setState({minimumContribution:event.target.value})

Refer that function in onChange

onChange={this.handleChange}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You should use async onSubmit (event) { and not onSubmit = async event => {.

The former declares a method while the latter initializes a class property with an arrow function. Arrow functions don't have their own this (and no, binding won't change that) so the surrounding scope's this will be used, which will be the global scope (the class block isn't a scope per se as it cannot contain code), and the global scope's this is undefined, and therefore, your code currently crashes on accessing this.setState.

about 4 years ago · Juan Pablo Isaza Denunciar

0

remove this.onSubmit = this.onSubmit.bind(this); from the constructor if you want to use class properties. for more information, see How to use arrow functions (public class fields) as class methods?

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda